package com.up4years.dataapi.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.up4years.controller.base.BaseController;
import com.up4years.dataapi.entity.base.Page;
import com.up4years.dataapi.service.system.OrderService;
import com.up4years.utils.ParamUtils;

@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {
	@Autowired
	private OrderService orderService;
	/**
	 * 查询全部
	 * @param request
	 * @return
	 */
	@RequestMapping("/list.htm")
	public ModelAndView list(HttpServletRequest request){
		Map<String,Object> parameters = handleParameters(request);
 		Page<Map<String,Object>> page = this.getPage(request);
		page = this.orderService.pageList(page, parameters);
 		return new ModelAndView(new MappingJacksonJsonView(),this.getSuccessResult(page));
	
	}
	/**
	 * 聚合查询
	 * @param request
	 * @return
	 */
	@RequestMapping("/query.htm")
	public ModelAndView query(HttpServletRequest request){
		Map<String,Object> parameters = handleParameters(request);
		String accept_user = (String)parameters.get("accept_user");
 		@SuppressWarnings("unchecked")
		List<String> status = (List<String>) parameters.get("status");
 		@SuppressWarnings("unchecked")
		List<String> order_type = (List<String>) parameters.get("order_type");
		if(StringUtils.isBlank(accept_user)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("[accept_user]物流员id不可为空！"));
		}
		if(status==null||status.size()==0){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("[status]订单状态参数不可为空！"));
		}
		if(order_type==null||order_type.size()==0){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("[order_type]订单类型参数不可为空！"));
		}
		List<String> allStatus = new ArrayList<String>();
		allStatus.addAll(Arrays.asList(new String[]{"1","2","3","4"}));
		parameters.put("accept_user", accept_user);
		Page<Map<String,Object>> page = this.getPage(request);
		page = this.orderService.pageList(page, parameters);
		return new ModelAndView(new MappingJacksonJsonView(),this.getSuccessResult(page));
	}
	/**
	 * 查询可接单
	 * @param request
	 * @return
	 */
	@RequestMapping("/available.htm")
	public ModelAndView available(HttpServletRequest request){
		Map<String,Object> parameters = handleParameters(request);
 		List<String> status = new ArrayList<String>();
		status.add("1");
		parameters.put("status", status);
		Page<Map<String,Object>> page = this.getPage(request);
		page = this.orderService.pageList(page, parameters);
		return new ModelAndView(new MappingJacksonJsonView(),this.getSuccessResult(page));
	}

	/**
	 * 查询已接单 不含已取件和已完成
	 * @param request
	 * @return
	 */
	@RequestMapping("/accepted.htm")
	public ModelAndView accepted(HttpServletRequest request){
		Map<String,Object> parameters = handleParameters(request);
		String accept_user = (String)parameters.get("accept_user");
		if(StringUtils.isBlank(accept_user)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("[accept_user]物流员id不可为空！"));
		}
		List<String> status = new ArrayList<String>();
		status.add("2");
		parameters.put("status", status);
		parameters.put("accept_user", accept_user);
		Page<Map<String,Object>> page = this.getPage(request);
		page = this.orderService.pageList(page, parameters);
		return new ModelAndView(new MappingJacksonJsonView(),this.getSuccessResult(page));
	}
	/**
	 * 查询已完成
	 * @param request
	 * @return
	 */
	@RequestMapping("/done.htm")
	public ModelAndView done(HttpServletRequest request){
		Map<String,Object> parameters = handleParameters(request);
		String accept_user = (String)parameters.get("accept_user");
		if(StringUtils.isBlank(accept_user)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("[accept_user]物流员id不可为空！"));
		}
		List<String> status = new ArrayList<String>();
		status.add("4");
		parameters.put("status", status);
		parameters.put("accept_user", accept_user);
		Page<Map<String,Object>> page = this.getPage(request);
		page = this.orderService.pageList(page, parameters);
		return new ModelAndView(new MappingJacksonJsonView(),this.getSuccessResult(page));
	}
	/**
	 * 接单
	 * @param request
	 * @return
	 */
	@RequestMapping("/accept.htm")
	public ModelAndView accept(HttpServletRequest request){
		Map<String,Object> parameters = ParamUtils.convert2Map(request);
		String order_no = (String)parameters.get("order_no");
		String accept_user = (String)parameters.get("accept_user");
		String accept_user_name = (String)parameters.get("accept_user_name");
		if(StringUtils.isBlank(order_no)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("[order_no]订单号不可为空！"));
		}
		if(StringUtils.isBlank(accept_user)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("[accept_user]物流员id不可为空！"));
		}
		if(StringUtils.isBlank(accept_user_name)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("[accept_user_name]物流员姓名不可为空！"));
		}
		parameters.put("status", 2);
		parameters.put("accept_user", accept_user);
		parameters.put("accept_user_name", accept_user_name);
		parameters.put("accept_time", new Date());
		boolean result = this.orderService.accept(parameters);
		return new ModelAndView(new MappingJacksonJsonView(),this.getSuccessResult(result));
	}
	/**
	 * 查询详情
	 * @param request
	 * @return
	 */
	@RequestMapping("/getByOrderNo.htm")
	public ModelAndView getByOrderNo(HttpServletRequest request){
		Map<String,Object> parameters = ParamUtils.convert2Map(request);
		String order_no = (String)parameters.get("order_no");
 		if(StringUtils.isBlank(order_no)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("[order_no]订单号不可为空！"));
		}
		Map<String,Object>  result = this.orderService.getByOrderNo(order_no);
		return new ModelAndView(new MappingJacksonJsonView(),this.getSuccessResult(result));	
	}
	/**
	 * 取货
	 * @param request
	 * @return
	 */
	@RequestMapping("/take.htm")
	public ModelAndView take(HttpServletRequest request){
		Map<String,Object> parameters = ParamUtils.convert2Map(request);
		String order_no = (String)parameters.get("order_no");
		if(StringUtils.isBlank(order_no)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("[order_no]订单号不可为空！"));
		}
		boolean  result = this.orderService.take(order_no);
		return new ModelAndView(new MappingJacksonJsonView(),this.getSuccessResult(result));	
	}
	/**
	 * 取货
	 * @param request
	 * @return
	 */
	@RequestMapping("/arrive.htm")
	public ModelAndView arrive(HttpServletRequest request){
		Map<String,Object> parameters = ParamUtils.convert2Map(request);
		String order_no = (String)parameters.get("order_no");
		String finish_code = (String)parameters.get("finish_code");
		String accept_user = (String)parameters.get("accept_user");
		if(StringUtils.isBlank(accept_user)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("[accept_user]送货员id不可为空！"));
		}
		if(StringUtils.isBlank(order_no)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("[order_no]订单号不可为空！"));
		}
		if(StringUtils.isBlank(finish_code)){
			return new ModelAndView(new MappingJacksonJsonView(),this.getFailResult("[finish_code]订单送达确认码不可为空！"));
		}
		boolean  result = this.orderService.arrive(order_no,accept_user,finish_code);
		return new ModelAndView(new MappingJacksonJsonView(),this.getSuccessResult(result));	
	}
	
	/**
	 * 订单类型order_type[0,表示超市订单，1，表示代取快递订单]
	 * order_typey以数组的形式 或逗号分割的形式
	 * @param request
	 * @return
	 */
	private Map<String,Object> handleParameters(HttpServletRequest request){
		Map<String,Object> parameters= ParamUtils.convert2Map(request);
 		/**将参数order_type转换为list**/
 		List<String> order_type = new ArrayList<String>();
 		List<String> status = new ArrayList<String>();
		if(parameters.get("order_type") instanceof String){
			String temp = (String)parameters.get("order_type");
			if(temp.contains(",")){
				String[] array = temp.split(",");
				for(String s : array){
					if(StringUtils.isNotBlank(s))order_type.add(s);
				}
 			}else{
				if(StringUtils.isNotBlank(temp))order_type.add(temp);				
			}
		}else if(parameters.get("order_type") instanceof String[]){
			String[] temp = (String[]) parameters.get("order_type");
			for(String s : temp){
				if(StringUtils.isNotBlank(s))order_type.add(s);
			}
 		}
		if(parameters.get("status") instanceof String){
			String temp = (String)parameters.get("status");
			if(temp.contains(",")){
				String[] array = temp.split(",");
				for(String s : array){
					if(StringUtils.isNotBlank(s))status.add(s);
				}
 			}else{
				if(StringUtils.isNotBlank(temp))status.add(temp);				
			}
		}else if(parameters.get("status") instanceof String[]){
			String[] temp = (String[]) parameters.get("status");
			for(String s : temp){
				if(StringUtils.isNotBlank(s))status.add(s);
			}
		}
		parameters.put("order_type", order_type);
		parameters.put("status", status);
		return parameters;
	}
}
